package com.example.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.crm.entity.Contract;
import com.example.crm.service.ContractService;
import com.example.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合同API控制器
 */
@RestController
@RequestMapping("/api/contract")
public class ContractController {

    @Autowired
    private ContractService contractService;

    /**
     * 分页查询合同列表
     */
    @GetMapping("/list")
    public Result<IPage<Contract>> list(@RequestParam(required = false) Map<String, Object> params) {
        if (params == null) {
            params = new HashMap<>();
        }
        IPage<Contract> page = contractService.listContractsByPage(params);
        return Result.success(page);
    }

    /**
     * 根据客户ID获取合同列表
     */
    @GetMapping("/customer/{customerId}")
    public Result<List<Contract>> getByCustomerId(@PathVariable Long customerId) {
        List<Contract> contracts = contractService.getContractsByCustomerId(customerId);
        return Result.success(contracts);
    }

    /**
     * 根据销售机会ID获取合同列表
     */
    @GetMapping("/opportunity/{opportunityId}")
    public Result<List<Contract>> getByOpportunityId(@PathVariable Long opportunityId) {
        List<Contract> contracts = contractService.getContractsByOpportunityId(opportunityId);
        return Result.success(contracts);
    }

    /**
     * 获取合同详情
     */
    @GetMapping("/{id}")
    public Result<Contract> getById(@PathVariable Long id) {
        Contract contract = contractService.getContractDetail(id);
        return Result.success(contract);
    }

    /**
     * 新增合同
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Contract contract) {
        contract.setCreateTime(LocalDateTime.now());
        contract.setUpdateTime(LocalDateTime.now());
        return Result.success(contractService.addContract(contract));
    }

    /**
     * 更新合同
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Contract contract) {
        contract.setUpdateTime(LocalDateTime.now());
        return Result.success(contractService.updateContract(contract));
    }

    /**
     * 更新合同状态
     */
    @PostMapping("/status/{id}")
    public Result<Boolean> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        return Result.success(contractService.updateContractStatus(id, status));
    }

    /**
     * 删除合同
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(contractService.removeById(id));
    }

    /**
     * 获取合同统计信息
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        return Result.success(contractService.getContractStatistics());
    }

    /**
     * 获取合同状态分布
     */
    @GetMapping("/status-distribution")
    public Result<Map<String, Object>> getStatusDistribution() {
        return Result.success(contractService.getContractStatusDistribution());
    }

    /**
     * 获取合同金额趋势
     */
    @GetMapping("/amount-trend")
    public Result<List<Map<String, Object>>> getAmountTrend(@RequestParam(required = false) Integer months) {
        return Result.success(contractService.getContractAmountTrend(months));
    }
} 